"""
@File         : route_depend.py
@Time         : 2024/05/08 16:38:59
@Author       : Qrj
@Email        : 923207736@qq.com
@Description  : 路由依赖(用于维持本业务的路由部分整洁)
"""

from typing import Annotated

from fastapi import Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession

from src.business.admin.system.notice.schemas.out import (
    NoticeCompleteOutSchema,
    NoticeNoContentOutSchema,
)
from src.business.admin.system.notice.schemas.validate import (
    NoticeCreateValidateSchema,
    NoticeDeleteValidateSchema,
    NoticeListSearchValidateSchema,
    NoticeUpdateStatusValidateSchema,
    NoticeUpdateValidateSchema,
)
from src.business.admin.system.notice.service import NoticeService
from src.business.admin.system.oauth2.token.dependencies.no_introduction import (
    GetTenantIdFromAccessTokenDependencies,
    GetUserNameFromAccessTokenDependencies,
)
from src.dependencies.no_introduction import depend_get_master_db_session
from src.schemas.validate import (
    OrderBySearchValidateSchema,
    PaginationSearchValidateSchema,
)


async def route_depend_get_notice(
    notice_id: Annotated[
        int, Annotated[int, Query(..., ge=1, description="通知公告ID")]
    ],
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> NoticeCompleteOutSchema:
    """查询单个通知公告的路由依赖

    Args:
        notice_id (Annotated[int, Depends): 通知公告ID
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        NoticeCompleteOutSchema: 通知公告信息
    """
    notice: NoticeCompleteOutSchema = await NoticeService.get_notice_use_id(
        db_session,
        tenant_id,
        notice_id,
        raise_disabled_exception=False,
        raise_expired_exception=False,
    )
    return notice


async def route_depend_list_notice(
    search_schema: NoticeListSearchValidateSchema,
    pagination_schema: PaginationSearchValidateSchema,
    order_schema: OrderBySearchValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> tuple[list[NoticeNoContentOutSchema], int]:
    """查询通知公告列表(无公告内容)的路由依赖

    Args:
        search_schema (Annotated[NoticeListSearchValidateSchema, Depends): 查询参数
        pagination_schema (Annotated[PaginationSearchValidateSchema, Depends): 分页参数
        order_schema (Annotated[OrderBySearchValidateSchema, Depends): 排序参数
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        tuple[list[NoticeNoContentOutSchema], int]: 查询结果
    """
    # 注解
    search_pagination_result: list[NoticeNoContentOutSchema]
    search_total: int
    # 列表结果查询
    (
        search_pagination_result,
        search_total,
    ) = await NoticeService.list_search_no_content_notice(
        db_session, tenant_id, search_schema, pagination_schema, order_schema
    )
    return search_pagination_result, search_total


async def route_depend_list_forgeground_notice(
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> list[NoticeCompleteOutSchema]:
    """获取前台通知公告列表的依赖

    Args:
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        list[NoticeCompleteOutSchema]: 查询结果
    """
    # 查询
    result = await NoticeService.list_get_forgeground_notice(db_session, tenant_id)
    return result


async def route_depend_list_background_notice(
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> list[NoticeCompleteOutSchema]:
    """获取后台通知公告列表的依赖

    Args:
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        list[NoticeCompleteOutSchema]: 查询结果
    """
    # 查询
    result = await NoticeService.list_get_background_notice(db_session, tenant_id)
    return result


async def route_depend_create_notice(
    create_schema: NoticeCreateValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> int:
    """创建通知公告的依赖

    Args:
        create_schema (NoticeCreateValidateSchema): 创建所需的参数
        tenant_id: 当前token指向的租户ID
        username: 当前token指向的通知公告名
        db_session: 数据库连接池

    Returns:
        int: 创建的sql的主键ID
    """
    # 创建通知公告
    created_id = await NoticeService.create_notice(
        db_session, tenant_id, create_schema, creator_username=username
    )
    return created_id


async def route_depend_update_notice(
    update_schema: NoticeUpdateValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """修改通知公告的路由依赖

    Args:
        update_schema (NoticeUpdateValidateSchema): 修改所需的参数
        tenant_id: 当前token指向的租户ID
        username: 当前token指向的通知公告名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await NoticeService.update_notice(
        db_session, tenant_id, update_schema, updater_username=username
    )


async def route_depend_update_notice_status(
    update_schema: NoticeUpdateStatusValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """单独修改通知公告状态的路由依赖

    Args:
        update_schema (NoticeUpdateStatusValidateSchema): 修改所需的参数
        tenant_id: 当前token指向的租户ID
        username: 当前token指向的通知公告名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await NoticeService.update_notice_status(
        db_session, tenant_id, update_schema, updater_username=username
    )


async def route_depend_delete_notice(
    delete_schema: NoticeDeleteValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """删除通知公告的路由依赖

    Args:
        delete_schema (NoticeDeleteValidateSchema): 删除所需的参数
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 删除操作
    await NoticeService.delete_notice(db_session, tenant_id, delete_schema)
